package com.gitee.secretopen.sso.service.impl;

import cn.chenc.framework.core.model.request.RequestHolder;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.core.model.response.ResultUtil;
import cn.chenc.framework.core.util.IDUtils;
import cn.chenc.framework.core.util.JsonUtils;
import cn.chenc.framework.excel.util.EasyExcelUtil;
import cn.chenc.framework.mybatisplus.model.BaseConditionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.secretopen.sso.common.util.Query;
import com.gitee.secretopen.sso.component.TokenService;
import com.gitee.secretopen.sso.controller.form.PageInfo;
import com.gitee.secretopen.sso.db.pojo.User;
import com.gitee.secretopen.sso.excelmodel.SiteUpload;
import com.gitee.secretopen.sso.excelmodel.UserUpload;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.secretopen.sso.db.dao.SiteMapper;
import com.gitee.secretopen.sso.db.pojo.Site;
import com.gitee.secretopen.sso.service.SiteService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author Xjt
 * @Date 2021/2/26 18:53
 * @Version 1.0
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements SiteService{

    @Autowired
    private TokenService tokenService;

    @Override
    public PageInfo<Site> page(Site params) {
        IPage<Site> page = this.page(
                new Query<Site>().getPage(params),
                new QueryWrapper<Site>()
                        .eq(!StringUtils.isEmpty(params.getSiteName()),
                        "site_name",params.getSiteName())
                        .between(!ObjectUtils.isEmpty(params.getStartDate()),
                        "create_time",params.getStartDate(),params.getEndDate())
                        .eq("is_delete",0));
        return PageInfo.getInstance(page);
    }

    @Override
    public PageInfo<Site> getByName(Site params) {
        IPage<Site> page = this.page(
                new Query<Site>().getPage(params),
                new QueryWrapper<Site>().like("site_name",params.getSiteName())
        );
        return PageInfo.getInstance(page);
    }

    @Override
    @Transactional
    public boolean addSite(Site site){
        HttpServletRequest request = RequestHolder.getRequest();
        User loginUser=tokenService.getAdminLoginUser(request);
        site.setSiteId("secret_sso_"+ IDUtils.genItemId());
        site.setSiteName(site.getSiteName());
        site.setCreateTime(LocalDateTime.now());
        site.setCreateBy(loginUser.getName());
        site.setUpdateTime(LocalDateTime.now());
        site.setUpdateBy(loginUser.getName());
        site.setIsDelete(0);
        return this.save(site);
    }

    @Override
    public List<Site> downloadSites() {
        List<Site> siteList = this.list();
        return siteList;
    }

    @Override
    public ResponseVO upload(MultipartFile file) throws Exception{
        HttpServletRequest request = RequestHolder.getRequest();
        User loginUser=tokenService.getAdminLoginUser(request);
        InputStream inputStream = file.getInputStream();
        List<Object> list = EasyExcelUtil.readExcel(inputStream, SiteUpload.class);
        String listJson= JsonUtils.objectToJson(list);
        List<Site> siteList=JsonUtils.jsonToList(listJson,Site.class);
        for(Site site:siteList){
            site.setSiteId("secret_sso_"+ IDUtils.genItemId());
            site.setCreateTime(LocalDateTime.now());
            site.setCreateBy(loginUser.getName());
            site.setUpdateTime(LocalDateTime.now());
            site.setUpdateBy(loginUser.getName());
            site.setIsDelete(0);
        }
        this.saveBatch(siteList);
        return ResultUtil.success("导入成功");
    }
}
